const api = require("../../utils/api")
const http = require("../../utils/http")
// pages/listDetail/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabledBtn:false,
        showPopup: false,
        showOverPopup: false,
        info: "",
        docList: [],
        childInfo: {},
        child_id: "",
        user_id: "",
        showIfVipPopup: false
    },
    onCloseIfVipPopup() {
        this.setData({
            showIfVipPopup: false
        })
    },
    onClosePopup() {
        this.setData({
            showPopup: false,
            showOverPopup: false,
        })
    },
    init() {
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        this.setData({
            child_id: childInfo.id ? childInfo.id : 0,
            childInfo: childInfo,
            user_id: userInfo.id,
        })
    },
    /** 新建预约 */
    postResrve() {
        http.postHttp(api.postResrve, {
                "data": {
                    "child_id": this.data.child_id,
                    "user_id": this.data.user_id,
                    "tele_doc_id": this.data.info.id
                },
                "type": "1"
            },
            (result) => {
                if (result.code == 0) {
                    this.setData({
                        disabledBtn:false,
                        showPopup: true
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    /** 先检测是否可以预约 */
    handleYuyue() {
        if (!this.data.childInfo.if_vip) {
            this.setData({
                showIfVipPopup: true
            })
            return
        }
        this.setData({
            disabledBtn:true
        })
        http.postHttp(api.isIfBook, {
                rtype: "1",
                personal_id: this.data.childInfo.personal_id
            },
            (result) => {
                if (result.code == 0) {
                    
                    if (result.data.ifok) {
                        this.postResrve()
                    } else {
                        console.log("不能预约");
                        this.setData({
                            showOverPopup: true,
                            disabledBtn:false,
                        })
                    }
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    zhuanjiaDetailPage() {
        // wx.navigateTo({
        //     url: '/pages/docDetail/index',
        // })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let info = JSON.parse(options.item)
        let arr = info.file_id.split(',')
        let arr2 = info.file_name.split(';')
        let arr3 = info.file_position.split(';')
        let newArr1 = arr.map((avatar, i) => ({
            avatar,
            name: arr2[i],
            position: arr3[i]
        }));
        console.log(newArr1);
        this.setData({
            info: info,
            docList: newArr1,
            banner: http.report_host + '/getFile/' + info.background_id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.init()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})